package com.yanqu.road.server.cmd.activity.operamask;

import com.yanqu.road.entity.activity.operamask.OperaMaskSceneRank;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.activity.operamask.OperaMaskSceneRankListModel;
import com.yanqu.road.logic.pb.OperaMaskPb2;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.OperaMaskProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.operamask.CrossOperaMaskMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = CrossProtocol.C2_OPERA_MASK_GET_SCENE_RANK_LIST, desc = "")
public class CrossOperaMaskGetSceneRankListCmd extends CrossCommand {

    private int activityId;
    private int sceneId;
    private long score;
    private long userId;
    private long fromServerId;
    private int type;
    private int skillValue;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        OperaMaskProto.CrossOperaMaskGetSceneRankListReqMsg reqMsg = OperaMaskProto.CrossOperaMaskGetSceneRankListReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        sceneId = reqMsg.getSceneId();
        score = reqMsg.getScore();
        userId = packet.getPlayerId();
        fromServerId = serverId;
        type = reqMsg.getType();
        skillValue = reqMsg.getSkillValue();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        OperaMaskSceneRankListModel model = CrossOperaMaskMgr.getOperaMaskSceneRankListModel(activityId, type, sceneId);

        OperaMaskProto.OperaMaskGetSceneRankListRespMsg.Builder builder = OperaMaskProto.OperaMaskGetSceneRankListRespMsg.newBuilder();
        builder.setRet(0);
        builder.setSceneId(sceneId);
        for (OperaMaskSceneRank rank : model.getRankList()) {
            UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(rank.getUserId());
            if (userBaseInfo == null) {
                userBaseInfo = new UserBaseInfo();
            }
            builder.addRankList(OperaMaskPb2.parseOperaMaskGetSceneRankTemp(rank, PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo)));
        }
        builder.setMyRank(model.getMyRank(userId));
        builder.setMyValue(score);
        builder.setSkillValue(skillValue);
        YanQuMessage message = YanQuMessageUtils.buildMessage(ClientProtocol.U_OPERA_MASK_GET_SCENE_RANK_LIST, builder);
        MessageHelper.sendPacket(fromServerId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
